package com.ls.server.controller;

import com.ls.server.service.ChatAttachmentService;
import com.ls.server.vo.ChatAttachmentVO;
import com.ls.server.vo.Result;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 聊天附件控制器
 */
@RestController
@RequestMapping("/chat/attachment")
public class ChatAttachmentController {

    private final ChatAttachmentService chatAttachmentService;

    public ChatAttachmentController(ChatAttachmentService chatAttachmentService) {
        this.chatAttachmentService = chatAttachmentService;
    }

    /**
     * 上传文件
     * @param file 文件
     * @param sessionId 会话ID
     * @param userId 用户ID
     * @return 附件ID
     */
    @PostMapping("/upload")
    public Result<Long> upload(
            @RequestParam("file") MultipartFile file,
            @RequestParam("sessionId") String sessionId,
            @RequestParam("userId") Long userId) {
        return Result.success(chatAttachmentService.uploadAttachment(file, sessionId, userId));
    }

    /**
     * 获取消息的所有附件
     * @param messageId 消息ID
     * @return 附件列表
     */
    @GetMapping("/message/{messageId}")
    public Result<List<ChatAttachmentVO>> listByMessageId(@PathVariable Long messageId) {
        return Result.success(chatAttachmentService.listByMessageId(messageId));
    }

    /**
     * 获取会话的所有附件
     * @param sessionId 会话ID
     * @return 附件列表
     */
    @GetMapping("/session/{sessionId}")
    public Result<List<ChatAttachmentVO>> listBySessionId(@PathVariable String sessionId) {
        return Result.success(chatAttachmentService.listBySessionId(sessionId));
    }

    /**
     * 删除附件
     * @param id 附件ID
     * @return 操作结果
     */
    @DeleteMapping("/{id}")
    public Result<Boolean> delete(@PathVariable Long id) {
        return Result.success(chatAttachmentService.deleteAttachment(id));
    }
} 